﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using AgileTimeTracker.Client.Wpf.Common;
using AgileTimeTracker.Client.Wpf.Services;
using Microsoft.Win32;

namespace AgileTimeTracker.Client.Wpf
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnExit(ExitEventArgs e)
        {            
            base.OnExit(e);
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            var dataFile = Wpf.Properties.Settings.Default.ConfigInfoFile;
            if (string.IsNullOrWhiteSpace(dataFile))
            {
                var dialog = new SaveFileDialog();
                dialog.Title = "Select a data file.";
                dialog.DefaultExt = ".dat";
                var ok = dialog.ShowDialog();
                if (ok.GetValueOrDefault())
                {
                    dataFile = dialog.FileName;
                    Wpf.Properties.Settings.Default.ConfigInfoFile = dataFile;
                    Wpf.Properties.Settings.Default.Save();                    
                }

                if (string.IsNullOrWhiteSpace(dataFile))
                {
                    MessageBoxHelper.ShowAlert("Cannot continue without a data file.", "Error");
                    App.Current.Shutdown(-1);
                }
            }

            MainWindow = new MainWindow();
            MainWindow.Show();
        }
    }
}
